import java.util.Scanner;

public class PSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int choice = 0;
        while (choice != 5) {
            System.out.println("请选择操作：");
            System.out.println("1. 加密密码");
            System.out.println("2. 解密密码");
            System.out.println("3. 判断密码强度");
            System.out.println("4. 生成密码");
            System.out.println("5. 退出系统");
            choice = scanner.nextInt();
            switch (choice) {
                case 1 -> {
                    System.out.println("请输入要加密的密码：");
                    scanner.nextLine();
                    String encrypt = scanner.nextLine();
                    String encryptPassword = encryptPassword(encrypt);
                    System.out.println("加密后的密码为：" + encryptPassword + "\n");
                }
                case 2 -> {
                    System.out.println("请输入要解密的密码：");
                    scanner.nextLine();
                    String decrypt = scanner.nextLine();
                    String decryptPassword = decryptPassword(decrypt);
                    System.out.println("解密后的密码为：" + decryptPassword + "\n");
                }
                case 3-> {
                    System.out.println("请输入要判断强度的密码：");
                    scanner.nextLine();
                    String passwordToCheckStrength = scanner.nextLine();
                    System.out.println(checkPasswordStrength(passwordToCheckStrength));
                }
                case 4->
                        System.out.println("生成的密码为：" + generatePassword() + "\n");
                case 5 ->
                        System.out.println("已退出系统，谢谢使用！");
                default ->
                        System.out.println("请输入正确的选项！");
            }
        }
    }

    //1.加密
    private static String encryptPassword(String password) {
        StringBuilder ePassword = new StringBuilder();
        char[] chars = password.toCharArray();
        //偏移
        for (int i = 0; i < chars.length; i++) {
            int encryptedChar = (int) chars[i] + i + 1 + 3;
            ePassword.append((char)encryptedChar);
        }

        //将第一位和最后一位调换顺序
        char temp = ePassword.charAt(0);
        ePassword.setCharAt(0, ePassword.charAt(ePassword.length() - 1));
        ePassword.setCharAt(ePassword.length() - 1, temp);

        //反转
        return ePassword.reverse().toString();
    }

    //2.解密
    private static String decryptPassword(String ePassword) {
        //反转
        StringBuilder dPassword = new StringBuilder(ePassword).reverse();

        //将第一位和最后一位调换顺序
        char temp = dPassword.charAt(0);
        dPassword.setCharAt(0, dPassword.charAt(ePassword.length() - 1));
        dPassword.setCharAt(dPassword.length() - 1, temp);

        //偏移
        char[] chars = dPassword.toString().toCharArray();
        dPassword.setLength(0);
        for (int i = 0; i < chars.length; i++) {
            int decryptedChar = (int) chars[i] - i - 1 - 3;
            dPassword.append((char)decryptedChar);
        }
        return dPassword.toString();
    }

    //3.判断密码强度
    private static String  checkPasswordStrength(String password) {
        if (password.length() < 8 || password.matches("[0-9]+") || password.matches("[a-zA-Z]+")) {
            return "密码强度为：弱强度\n";
        } else if (password.matches(".*\\d.*") && password.matches(".*[a-z].*") && password.matches(".*[A-Z].*")){
            return "密码强度为：高强度\n";
        } else {
            return "密码强度为：中强度\n";
        }
    }

    //4.生成密码
    private static String generatePassword() {
        //包含数字、大小写字母的随机密码(强密码)
        String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder password = new StringBuilder();
        for (int i = 0; i < 16; i++) {
            int randomIndex = (int) (Math.random() * characters.length());
            password.append(characters.charAt(randomIndex));
        }
        return password.toString();
    }
}
//方伟铠到此一游

